﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Themtintuc : System.Web.UI.Page
{
    BUS_News objP = new BUS_News();
    BUS_Category objC = new BUS_Category();
    protected void Page_Load(object sender, EventArgs e)
    {

        Page.Title = "Thêm Tin Tức";
        if (!IsPostBack)
        {
            if (Session["AdminLogin"] != null)
            {
                LoadData();
            }
            else Response.Redirect("Dangnhap.aspx");
        }
    }

    public void LoadData()
    {
        try
        {
            linkQLTT.NavigateUrl = "Tintuc.aspx";
            linkthemtt.NavigateUrl = "Themtintuc.aspx";
            //load du lieu len dropdownlist loai san pham
            droploaitt.DataSource = objC.Category_getData();
            droploaitt.DataTextField = "Name";
            droploaitt.DataValueField = "Cgid";
            droploaitt.DataBind();
            //load du lieu len dropdownlist nha san xuat
            //dropnxs.DataSource = objM.Maker_getData();
            //dropnxs.DataTextField = "Name";
            //dropnxs.DataValueField = "ID";
            //dropnxs.DataBind();
        }
        catch
        {
        }
    }
    protected void btnthemsp_Click(object sender, EventArgs e)
    {
        DTO_News dtoP = new DTO_News();
        DTO_Category dtoC = new DTO_Category();
        //var maxOrder = objP.News_getMaxOrder();
        //int order = int.Parse(maxOrder.Rows[0]["Order"].ToString());
        DateTime strngay = DateTime.Today;
        if (txttentt.Text != "" && EditorSumary.Text != "")
        {
            dtoP.Title = txttentt.Text.Trim();
            dtoP.Contents = EditorSumary.Text.Trim();
            //dtoP.Price = float.Parse(txtgiasp.Text.Trim());
            dtoP.Date = strngay.ToString("MM/dd/yyyy");
            //dtoP.Order = order + 1;
            dtoP.Status = int.Parse(droptrangthai.SelectedValue.ToString());
            //dtoP.Maker_Id = int.Parse(dropnxs.SelectedValue.ToString());
            dtoP.Type = int.Parse(droploaitt.SelectedValue.ToString());
            if (uploadanhtt.FileName != string.Empty)
            {
                Session["isInsertPic"] = true;
                dtoP.Image = uploadanhtt.FileName.Substring(uploadanhtt.FileName.LastIndexOf(@"\") + 1);
                if (!UploadImage(dtoP.Image))
                    return;
            }
            else
            {
                Session["isInsertPic"] = false;
            }

            //if (uploadanhnho.FileName != string.Empty)
            //{
            //    Session["isInsertPic1"] = true;
            //    dtoP.ImageSmall = uploadanhnho.FileName.Substring(uploadanhnho.FileName.LastIndexOf(@"\") + 1);
            //    if (!UploadImageSmall(dtoP.ImageSmall))
            //        return;
            //}
            //else
            //{
            //    Session["isInsertPic1"] = false;
            //}
            if (!(bool)Session["isInsertPic"])
            {
                dtoP.Image = "";
            }
            //if (!(bool)Session["isInsertPic1"])
            //{
            //    dtoP.ImageSmall = "";
            //}
            //if (chkkhuyenmai.Checked)
            //{
            //    dtoP.PriceNew = float.Parse(txtkhuyenmai.Text.Trim());
            //}
            //else dtoP.PriceNew = 0;
            //if (dtoP.ImageSmall == "")
            //{
            //    Response.Write("<script>alert(\"Bạn chưa upload ảnh sản phẩm nhỏ\")</script>");
            //}
            //else
                if (dtoP.Image == "")
                {
                    Response.Write("<script>alert(\"Bạn chưa upload ảnh tin tức\")</script>");
                }
                else
                    if (objP.News_Insert(dtoP))
                    {
                        var maxPid = objP.News_getMaxPid();
                        int pid = int.Parse(maxPid.Rows[0]["id"].ToString());
                        Response.Write("<script>alert(\"Thêm tin tức thành công <..*!>\")</script>");
                        Response.Redirect("Xemtintuc.aspx?cid=" + dtoP.Type + "&id=" + pid);
                    }
                    else Response.Write("<script>alert(\"Thêm không thành công <..*!>\")</script>");
        }
        else
        {
            Response.Write("<script>alert(\"Vui lòng điền đầy đủ thông tin có (*)\")</script>");
        }
        //Response.Write("<script>alert(\"" + dtoP.Name + "," + dtoP.Detail + "," + dtoP.Price + "," + dtoP.ImageLarge + "," + dtoP.ImageSmall + "," + dtoP.PriceNew + "," + dtoP.Date + " <..*!>\")</script>");
    }

    //Phuong thuc upload anh
    public bool UploadImage(string imagelargename)
    {
        bool complete = false;
        //Lay ve ten file anh
        string fname = uploadanhtt.FileName;
        //Tach ten file
        string[] tokens = fname.Split(new char[] { '.' });
        //Luu tru dung luong file tinh bang byte
        int bytes = uploadanhtt.FileBytes.Length;
        //Kiem tra dung luong file anh
        if (bytes > (1024 * 1024))
        {
            lblargeerr.Text = "Dung lượng ảnh phải nhỏ hơn 4KB";
            return false;
        }
        //Kiem tra dang file upload
        if (tokens[tokens.Length - 1] == "jpg" || tokens[tokens.Length - 1] == "jpeg" || tokens[tokens.Length - 1] == "png" || tokens[tokens.Length - 1] == "gif")
        {
            //Khai bao bien luu tru duong dan den thu muc images
            string relPath = Server.MapPath("..\\.");
            //Khai bao bien luu tru duong dan tuyet doi
            string absolutePath = relPath + "\\Uploads\\News\\" + imagelargename;
            //Luu len server
            uploadanhtt.SaveAs(absolutePath);
            complete = true;
        }
        else
        {
            lblargeerr.Text = "Đây không phải file ảnh.";
            return false;
        }
        return complete;
    }
    //public bool UploadImageSmall(string imagesmallname)
    //{
    //    bool complete1 = false;
    //    //Lay ve ten file anh
    //    string fname1 = uploadanhnho.FileName;
    //    //Tach ten file
    //    string[] tokens1 = fname1.Split(new char[] { '.' });
    //    //Luu tru dung luong file tinh bang byte
    //    int bytes1 = uploadanhnho.FileBytes.Length;
    //    //Kiem tra dung luong file anh
    //    if (bytes1 > (1024 * 1024))
    //    {
    //        lbsmallerr.Text = "Dung lượng ảnh phải nhỏ hơn 4KB";
    //        return false;
    //    }
    //    //Kiem tra dang file upload
    //    if (tokens1[tokens1.Length - 1] == "jpg" || tokens1[tokens1.Length - 1] == "jpeg" || tokens1[tokens1.Length - 1] == "png" || tokens1[tokens1.Length - 1] == "gif")
    //    {
    //        //Khai bao bien luu tru duong dan den thu muc images
    //        string relPath1 = Server.MapPath("..\\.");
    //        //Khai bao bien luu tru duong dan tuyet doi
    //        string absolutePath1 = relPath1 + "\\Uploads\\Product\\Small\\" + imagesmallname;
    //        //Luu len server
    //        uploadanhnho.SaveAs(absolutePath1);
    //        complete1 = true;
    //    }
    //    else
    //    {
    //        lbsmallerr.Text = "Đây không phải file ảnh.";
    //        return false;
    //    }
    //    return complete1;
    //}
    //protected void chkkhuyenmai_CheckedChanged(object sender, EventArgs e)
    //{
    //    if (chkkhuyenmai.Checked == true)
    //    {
    //        txtkhuyenmai.Visible = true;
    //    }
    //    else txtkhuyenmai.Visible = false;
    //}
}